import 'dart:convert';

import 'package:lpinyin/lpinyin.dart';

String stringToBase64(String s) {
  var bytes = utf8.encode(s);
  String base64Str = base64.encode(bytes);
  return base64Str;
}

String base64ToString(String base64Str) {
  return utf8.decode(base64.decode(base64Str));
}

// /tmp/hello.txt => hello.txt
String getBaseName(String filePath) {
  final index = filePath.lastIndexOf("/");
  if (index == -1) {
    return filePath;
  }
  return filePath.substring(index);
}

String convertToPinyin(String text) {
  String result = "#";
  try {
    result = PinyinHelper.getFirstWordPinyin(text);
    int codeUnit = result.codeUnitAt(0);
    if (codeUnit >= 97 && codeUnit <= 122) {
      result = result[0].toUpperCase();
    } else {
      result = "#";
    }
  } catch (e) {}
  return result;
}

void main() {
  print(convertToPinyin("王学鹏"));
  print(convertToPinyin("110"));
  print(convertToPinyin("朴鹏"));
  print(convertToPinyin("id"));
  print(convertToPinyin("😁xx"));
}
